/*
    
    Copyright (C) 2011  Antoine

 	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
   	the Free Software Foundation; either version 3 of the License, or
   	(at your option) any later version.

   	This program is distributed in the hope that it will be useful,
   	but WITHOUT ANY WARRANTY; without even the implied warranty of
   	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   	GNU General Public License for more details.

   	You should have received a copy of the GNU General Public License
   	along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package subs;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SpinnerListener implements ChangeListener {

	MainView			vue;
	// Change
	Date				date_debut;
	Date				date_fin;
	long				duree_initiale;
	SimpleDateFormat	formatter	= new SimpleDateFormat("HH:mm:ss,SSS");

	public SpinnerListener(MainView vue1) {
		vue = vue1;
	}

	public void stateChanged(ChangeEvent evt) {

		Calendar calendar_debut = Calendar.getInstance();
		Calendar calendar_fin = Calendar.getInstance();

		JSpinner spinner = (JSpinner) evt.getSource();

		int rowIndex = vue.table.getSelectedRow();

		String value_date_debut = (String) vue.table.getValueAt(rowIndex, 1);
		String value_date_fin = (String) vue.table.getValueAt(rowIndex, 2);

		try {
			date_debut = formatter.parse(value_date_debut);
			date_fin = formatter.parse(value_date_fin);
			duree_initiale = date_fin.getTime() - date_debut.getTime();
		}
		catch (Exception ex) {
		}

		calendar_debut.setTime(date_debut);
		calendar_fin.setTime(date_fin);

		int duree_finale = Integer.parseInt(spinner.getValue().toString());

		if (duree_finale > duree_initiale) {

			duree_finale = (int) (Integer.parseInt(spinner.getValue().toString()) - duree_initiale);

			int duree_finale_moitie = duree_finale / 2;

			calendar_debut.set(Calendar.MILLISECOND, calendar_debut.get(Calendar.MILLISECOND) - duree_finale_moitie);
			date_debut = calendar_debut.getTime();
			vue.debut_sub.setText(formatter.format(date_debut));

			calendar_fin.set(Calendar.MILLISECOND, calendar_fin.get(Calendar.MILLISECOND) + duree_finale_moitie);
			date_fin = calendar_fin.getTime();
			//vue.fin_sub.setText(formatter.format(date_fin));

		}
		else if (duree_finale < duree_initiale) {

			duree_finale = (int) (duree_initiale - Integer.parseInt(spinner.getValue().toString()));

			int duree_finale_moitie = duree_finale / 2;

			calendar_debut.set(Calendar.MILLISECOND, calendar_debut.get(Calendar.MILLISECOND) + duree_finale_moitie);
			date_debut = calendar_debut.getTime();
			vue.debut_sub.setText(formatter.format(date_debut));

			calendar_fin.set(Calendar.MILLISECOND, calendar_fin.get(Calendar.MILLISECOND) - duree_finale_moitie);
			date_fin = calendar_fin.getTime();
			//vue.fin_sub.setText(formatter.format(date_fin));

		}
	}
}
